/**
 * Created by Freeman on 2016/8/9.
 */
import {COMMENT_LIST_SUCCESS,COMMENT_LIST_FAILURE,ADD_COMMENT_SUCCESS,ADD_REPLY_SUCCESS} from '../constants/ActionTypes'

const initialState = {
    isFetching: false,
    items: []
};

export default function commentList(state = initialState, action) {
    switch (action.type){
        case COMMENT_LIST_SUCCESS:
            return {
                ...state,
                isFetching:false,
                items:action.json.data
            };
        case COMMENT_LIST_FAILURE:
            return state;
        case ADD_COMMENT_SUCCESS:
            let _items = state.items;
            _items.push(action.comment);
            return {
                ...state,
                items:_items
            };
        case ADD_REPLY_SUCCESS:
            let items = state.items;
            items.map(item=>{
                if(item._id === action.cid){
                    item.replys = action.replys
                }
                return item
            });
            return {
                ...state,
                items:items
            };
        default :
            return state;
    }
}